/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IjinUsahaAngkutanOrangDialog.java
 *
 * Created on Sep 26, 2011, 4:43:30 AM
 */
package com.x3.dishub.ui;

import com.x3.dishub.dao.IjinUsahaAngkutanOrangDAO;
import com.x3.dishub.dao.PerusahaanDAO;
import com.x3.dishub.dao.WarnaDAO;
import com.x3.dishub.entity.IjinUsahaAngkutanOrang;
import com.x3.dishub.entity.Perusahaan;
import com.x3.dishub.entity.Warna;
import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import javax.swing.JColorChooser;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JRViewer;

/**
 *
 * @author Hendro Steven
 */
public class IjinUsahaAngkutanOrangDialog extends javax.swing.JDialog {

    private WarnaDAO wDao = (WarnaDAO) MainApps.appContext.getBean("warnaDAO");
    private Warna headerColor;
    private Warna backgroudColor;
    private IjinUsahaAngkutanOrangDAO dao = (IjinUsahaAngkutanOrangDAO) MainApps.appContext.getBean("ijinUsahaAngkutanOrangDAO");
    private PerusahaanDAO pDao = (PerusahaanDAO) MainApps.appContext.getBean("perusahaanDAO");
    private List<IjinUsahaAngkutanOrang> list;
    private IjinUsahaAngkutanOrang selected;
    private SimpleDateFormat formater = new SimpleDateFormat("dd/MM/yyyy");

    /** Creates new form IjinUsahaAngkutanOrangDialog */
    public IjinUsahaAngkutanOrangDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
        loadWarna();
        updateWarna();
        //this.getContentPane().setBackground(new java.awt.Color(51, 102, 255));
        loadIjin();
        loadPerusahaan();
    }

    private void loadWarna() {
        headerColor = wDao.getByNama("IjinUsahaAngkutanOrangDialogHd");
        if (headerColor == null) {
            headerColor = new Warna();
            headerColor.setName("IjinUsahaAngkutanOrangDialogHd");
            headerColor.setRgb(-16777012);
            wDao.insert(headerColor);
            loadWarna();
        }
        backgroudColor = wDao.getByNama("IjinUsahaAngkutanOrangDialogBg");
        if (backgroudColor == null) {
            backgroudColor = new Warna();
            backgroudColor.setName("IjinUsahaAngkutanOrangDialogBg");
            backgroudColor.setRgb(-13408513);
            wDao.insert(backgroudColor);
            loadWarna();
        }
    }

    private void updateWarna() {
        headerPanel.setBackground(new Color(headerColor.getRgb()));
        this.getContentPane().setBackground(new Color(backgroudColor.getRgb()));
    }

    private void loadIjin() {
        try {
            list = dao.getAllIjinUsahaAngkutanOrang();
            String title[] = {"Nomor", "Tanggal", "Perusahaan", "Pimpinan"};
            Object data[][] = new Object[list.size()][4];
            int row = 0;
            for (IjinUsahaAngkutanOrang i : list) {
                data[row][0] = i.getNomor();
                data[row][1] = formater.format(i.getTanggal());
                data[row][2] = i.getPerusahaan().getNama();
                data[row][3] = i.getPerusahaan().getNamaPimpinan();
                ++row;
            }
            TableModel model = new DefaultTableModel(data, title);
            tblIjin.setModel(model);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        headerPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblIjin = new javax.swing.JTable();
        btnCari = new javax.swing.JButton();
        tblRefresh = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        tglPermohonan = new org.jdesktop.swingx.JXDatePicker();
        txtNomorPermohonan = new javax.swing.JTextField();
        cmbPerusahaan = new javax.swing.JComboBox();
        tglSurat = new org.jdesktop.swingx.JXDatePicker();
        txtNomor = new javax.swing.JTextField();
        btnBuatBaru = new javax.swing.JButton();
        btnSimpan = new javax.swing.JButton();
        btnHapus = new javax.swing.JButton();
        btnCetak = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnuWarna = new javax.swing.JMenu();
        mnuWarnaHeader = new javax.swing.JMenuItem();
        mnuWarnaBack = new javax.swing.JMenuItem();
        mnuWarnaDefault = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Ijin Usaha Angkutan Orang");

        headerPanel.setBackground(new java.awt.Color(0, 0, 204));
        headerPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 20));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Ijin Usaha Angkutan Orang");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Pembuatan Ijin Usaha Angkutan Orang");

        javax.swing.GroupLayout headerPanelLayout = new javax.swing.GroupLayout(headerPanel);
        headerPanel.setLayout(headerPanelLayout);
        headerPanelLayout.setHorizontalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(headerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(headerPanelLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel2))
                    .addComponent(jLabel1))
                .addContainerGap(480, Short.MAX_VALUE))
        );
        headerPanelLayout.setVerticalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(headerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tblIjin.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblIjin.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblIjinMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblIjin);

        btnCari.setText("Pencarian Berdasarkan Nomor");
        btnCari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCariActionPerformed(evt);
            }
        });

        tblRefresh.setText("Refresh Tabel");
        tblRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tblRefreshActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 725, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addComponent(btnCari)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tblRefresh)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCari)
                    .addComponent(tblRefresh))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Daftar Perijinan", jPanel2);

        jLabel3.setText("Nomor Surat  :");

        jLabel4.setText("Tanggal Surat :");

        jLabel5.setText("Perusahaan :");

        jLabel6.setText("Nomor Surat Permohonan :");

        jLabel7.setText("Tanggal Surat Permohonan :");

        cmbPerusahaan.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        btnBuatBaru.setText("Buat Baru");
        btnBuatBaru.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuatBaruActionPerformed(evt);
            }
        });

        btnSimpan.setText("Simpan");
        btnSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimpanActionPerformed(evt);
            }
        });

        btnHapus.setText("Hapus");
        btnHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHapusActionPerformed(evt);
            }
        });

        btnCetak.setText("Cetak Ijin");
        btnCetak.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCetakActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tglPermohonan, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmbPerusahaan, javax.swing.GroupLayout.PREFERRED_SIZE, 483, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtNomorPermohonan, javax.swing.GroupLayout.PREFERRED_SIZE, 383, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tglSurat, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtNomor, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(btnCetak)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 425, Short.MAX_VALUE)
                        .addComponent(btnHapus)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBuatBaru)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSimpan)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNomor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tglSurat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(cmbPerusahaan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtNomorPermohonan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(tglPermohonan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 248, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBuatBaru)
                    .addComponent(btnSimpan)
                    .addComponent(btnHapus)
                    .addComponent(btnCetak))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Buat atau Ubah Perijinan (Detail)", jPanel3);

        mnuWarna.setText("Seting");

        mnuWarnaHeader.setText("Warna Header");
        mnuWarnaHeader.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuWarnaHeaderActionPerformed(evt);
            }
        });
        mnuWarna.add(mnuWarnaHeader);

        mnuWarnaBack.setText("Warna Background");
        mnuWarnaBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuWarnaBackActionPerformed(evt);
            }
        });
        mnuWarna.add(mnuWarnaBack);

        mnuWarnaDefault.setText("Warna Default");
        mnuWarnaDefault.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuWarnaDefaultActionPerformed(evt);
            }
        });
        mnuWarna.add(mnuWarnaDefault);

        jMenuBar1.add(mnuWarna);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(headerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 750, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(headerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCariActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCariActionPerformed
        try {
            String nomor = JOptionPane.showInputDialog(this, "Nomor Ijin : ");
            list = dao.getByNomor(nomor);
            String title[] = {"Nomor", "Tanggal", "Perusahaan", "Pimpinan"};
            Object data[][] = new Object[list.size()][4];
            int row = 0;
            for (IjinUsahaAngkutanOrang i : list) {
                data[row][0] = i.getNomor();
                data[row][1] = formater.format(i.getTanggal());
                data[row][2] = i.getPerusahaan().getNama();
                data[row][3] = i.getPerusahaan().getNamaPimpinan();
                ++row;
            }
            TableModel model = new DefaultTableModel(data, title);
            tblIjin.setModel(model);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnCariActionPerformed

    private void tblRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tblRefreshActionPerformed
        loadIjin();
    }//GEN-LAST:event_tblRefreshActionPerformed

    private void loadPerusahaan() {
        try {
            cmbPerusahaan.removeAllItems();
            for (Perusahaan p : pDao.getAllPerusahaan()) {
                cmbPerusahaan.addItem(p);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    private int getCmbPerusahaanIndex(Perusahaan p) {
        int index = 0;
        for (Perusahaan tmp : pDao.getAllPerusahaan()) {
            if (tmp.getId() == p.getId()) {
                break;
            }
            ++index;
        }
        return index;
    }

    private void tblIjinMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblIjinMouseClicked
        selected = list.get(tblIjin.getSelectedRow());
        txtNomor.setText(selected.getNomor());
        tglSurat.setDate(selected.getTanggal());
        cmbPerusahaan.setSelectedIndex(getCmbPerusahaanIndex(selected.getPerusahaan()));
        txtNomorPermohonan.setText(selected.getNomorSuratPermohonan());
        tglPermohonan.setDate(selected.getTglSuratPermohonan());
    }//GEN-LAST:event_tblIjinMouseClicked

    private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHapusActionPerformed
        try {
            if (selected != null) {
                dao.delete(selected);
                txtNomor.setText("");
                tglSurat.setDate(new Date());
                cmbPerusahaan.setSelectedItem(null);
                txtNomorPermohonan.setText("");
                tglPermohonan.setDate(new Date());
                selected = null;
                loadIjin();
                JOptionPane.showMessageDialog(this, "Data terhapus");
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnHapusActionPerformed

    private void btnBuatBaruActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuatBaruActionPerformed
        selected = null;
        txtNomor.setText("");
        tglSurat.setDate(new Date());
        cmbPerusahaan.setSelectedItem(null);
        txtNomorPermohonan.setText("");
        tglPermohonan.setDate(new Date());
        txtNomor.requestFocus();
    }//GEN-LAST:event_btnBuatBaruActionPerformed

    private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimpanActionPerformed
        try {
            if (!txtNomor.getText().isEmpty() && !txtNomorPermohonan.getText().isEmpty()) {
                if (selected == null) {
                    IjinUsahaAngkutanOrang i = new IjinUsahaAngkutanOrang();
                    i.setNomor(txtNomor.getText());
                    i.setTanggal(tglSurat.getDate());
                    i.setPerusahaan((Perusahaan) cmbPerusahaan.getSelectedItem());
                    i.setTglSuratPermohonan(tglPermohonan.getDate());
                    i.setNomorSuratPermohonan(txtNomorPermohonan.getText());
                    dao.insert(i);
                } else {
                    selected.setNomor(txtNomor.getText());
                    selected.setTanggal(tglSurat.getDate());
                    selected.setPerusahaan((Perusahaan) cmbPerusahaan.getSelectedItem());
                    selected.setTglSuratPermohonan(tglPermohonan.getDate());
                    selected.setNomorSuratPermohonan(txtNomorPermohonan.getText());
                    dao.update(selected);
                    selected = null;
                }
                loadIjin();
                txtNomor.setText("");
                tglSurat.setDate(new Date());
                cmbPerusahaan.setSelectedItem(null);
                txtNomorPermohonan.setText("");
                tglPermohonan.setDate(new Date());
                JOptionPane.showMessageDialog(this, "Data tersimpan");
            } else {
                JOptionPane.showMessageDialog(this, "Masukan data dengan lengkap");
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnSimpanActionPerformed

    private void btnCetakActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCetakActionPerformed
        try {
            if (selected != null) {
                //ambil parameter
                Map mp = new HashMap();
                mp.put("nomor", selected.getNomor());

                DataSource ds = (DataSource) MainApps.appContext.getBean("dataSource");
                JDialog printView = new JDialog();
                JasperPrint print = JasperFillManager.fillReport("print/ijinUsahaUmum.jasper",
                        mp, ds.getConnection());
                JRViewer viewer = new JRViewer(print);
                printView.getContentPane().add(viewer);
                printView.setModal(false);
                printView.setTitle("Print Ijin Usaha");
                printView.setSize(800, 500);
                printView.setVisible(true);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnCetakActionPerformed

    private void mnuWarnaHeaderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuWarnaHeaderActionPerformed
        Color tmp = JColorChooser.showDialog(this, "Ganti Warna Header", headerPanel.getBackground());
        headerColor.setRgb(tmp.getRGB());
        wDao.update(headerColor);
        updateWarna();
    }//GEN-LAST:event_mnuWarnaHeaderActionPerformed

    private void mnuWarnaBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuWarnaBackActionPerformed
        Color tmp = JColorChooser.showDialog(this, "Ganti Warna Backgroud", this.getContentPane().getBackground());
        backgroudColor.setRgb(tmp.getRGB());
        wDao.update(backgroudColor);
        updateWarna();
    }//GEN-LAST:event_mnuWarnaBackActionPerformed

    private void mnuWarnaDefaultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuWarnaDefaultActionPerformed
        headerColor.setRgb(-16777012);
        backgroudColor.setRgb(-13408513);
        wDao.update(headerColor);
        wDao.update(backgroudColor);
        updateWarna();
    }//GEN-LAST:event_mnuWarnaDefaultActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuatBaru;
    private javax.swing.JButton btnCari;
    private javax.swing.JButton btnCetak;
    private javax.swing.JButton btnHapus;
    private javax.swing.JButton btnSimpan;
    private javax.swing.JComboBox cmbPerusahaan;
    private javax.swing.JPanel headerPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JMenu mnuWarna;
    private javax.swing.JMenuItem mnuWarnaBack;
    private javax.swing.JMenuItem mnuWarnaDefault;
    private javax.swing.JMenuItem mnuWarnaHeader;
    private javax.swing.JTable tblIjin;
    private javax.swing.JButton tblRefresh;
    private org.jdesktop.swingx.JXDatePicker tglPermohonan;
    private org.jdesktop.swingx.JXDatePicker tglSurat;
    private javax.swing.JTextField txtNomor;
    private javax.swing.JTextField txtNomorPermohonan;
    // End of variables declaration//GEN-END:variables
}
